package com.hex.ds.hdtp.core.web.data.transfer.controller;

import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.enums.DataTransferTypeEnum;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.request.DataTransferTaskRequestDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.response.DataTransferDownwardNumResponseDto;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferTaskService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.transfer.converter.IDataTransferTaskVoConverter;
import com.hex.ds.hdtp.core.web.data.transfer.vo.DataTransferTaskVo;
import com.hex.ds.hdtp.core.web.data.transfer.vo.query.DataTransferTaskQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.web.data.transfer.controller
 * @ClassName DataTransferTaskController
 * @Description: 数据迁移任务-WEB接口
 * @Author: wz.li
 * @Date 2023/8/17 20:32
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "02-数据迁移：任务")
@RequestMapping("/api/data/transfer/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DataTransferTaskController {

    private final IDataTransferTaskService dataTransferTaskService;

    private final IDataTransferTaskVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [dataTransferTaskQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/17 20:36 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("数据迁移任务-分页查询")
    public ResponseEntity queryListByPage(DataTransferTaskQueryVo dataTransferTaskQueryVo, PageVo pageVo) {
        log.debug("进入到【DataTransferTaskController.queryListByPage】方法,参数：{}" + dataTransferTaskQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        DataTransferTaskRequestDto requestDto = converter.toDtoForQueryVo(dataTransferTaskQueryVo);
        return ResponseEntity.ok(dataTransferTaskService.queryListByPage(requestDto, pageDto));
    }

    /*
     * @Method: add <br>
     * @Param: [dataTransferTaskVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：数据迁移任务-新增并同步<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/18 15:35 <br>
     * @Version： V2.0.2<br>
     */
    @PostMapping
    @Log("数据迁移任务-新增并同步")
    @ApiOperation("数据迁移任务-新增并同步")
    public ResponseEntity addWithBackId(@RequestBody DataTransferTaskVo dataTransferTaskVo) {
        log.info("进入到【DataTransferTaskController.addWithBackId】方法,参数：{}", dataTransferTaskVo.toString());
        return ResponseEntity.ok(dataTransferTaskService.addWithBackId(converter.toDto(dataTransferTaskVo)));
    }

    /**
     * @Method deleteById
     * @Param
     * @Param pkIds
     * @Return org.springframework.http.ResponseEntity
     * @Description 批量删除操作
     * @Author xin.zhao
     * @Date 2023/11/1 14:26
     * @Version V1.0
     */
    @Log("数据迁移任务-批量删除")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("数据迁移任务-批量删除")
    public ResponseEntity deleteById(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【DateTransferTaskController.deleteById】方法,参数：" + pkIds);
        dataTransferTaskService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    @GetMapping("/downnum")
    @Log("数据迁移-下数")
    @ApiOperation("数据迁移-下数")
    @AnonymousAccess
    public ResponseEntity queryListByType() {
        DataTransferTaskVo dataTransferTaskVo = new DataTransferTaskVo();
        dataTransferTaskVo.setTransferType(DataTransferTypeEnum.DOWNWARD_NUM.getValue());
        List<DataTransferDownwardNumResponseDto> dataTransferDownwardNumRequests = dataTransferTaskService.queryDownNum(converter.toDto(dataTransferTaskVo));
        return ResponseEntity.ok(dataTransferDownwardNumRequests);
    }

    @PutMapping
    @Log("数据迁移任务-保存")
    @ApiOperation("数据迁移任务-保存")
    public ResponseEntity update(@RequestBody DataTransferTaskVo dataTransferTaskVo) {
        log.info("进入到【DataTransferTaskController.update】方法,参数：{}", dataTransferTaskVo.toString());
        dataTransferTaskService.modifyById(converter.toDto(dataTransferTaskVo));
        return ResponseEntity.ok("保存成功");
    }

}
